<?php
/**
 * @author trongkhoi
 * @since Jul 6, 2013 - 6:03:36 PM
 */
Yii::import('zii.widgets.grid.CButtonColumn');

class MyButtonColumn extends CButtonColumn
{

    public $controller;
    /*
     * get controller name to create button link
     */
    public $buttons;
    public $template = '{view} {update} {delete}';
    public $viewUrl = FALSE;
    public $editUrl = FALSE;
    public $deleteUrl = FALSE;

    function init()
    {
        $limitArray = array(null=>Yii::t('user','Limit'),10=>10, 20=>20, 50=>50, 100=>100, 500=>500, 1000=>1000);
        $this->header = CHtml::dropDownList('pageSize', getState('pageSize', Yii::app()->params['defaultPageSize']), $limitArray, array(
                          'onchange'=>"$.fn.yiiGridView.update('data-grid',{data:{pageSize: $(this).val()}})"));
        if ($this->viewUrl)
        {
            $this->buttons['view'] = array(
                'icon' => 'icon-zoom-in',
                'options' => array('class' => 'btn btn-success'),
                'label' => Yii::t('user', 'View'),
                'url' => $this->viewUrl,
                'imageUrl' => false,
                'label' => '<i class="icon-search"></i>',
                'options' => array(
                    'class' => 'btn btn-info',
                    'target' => '_blank',
                    'title' => Yii::t('user', 'View')
                ),
            );
        }
        if ($this->editUrl)
        {
            $this->buttons['update'] = array(
                'icon' => 'icon-edit',
                'options' => array('class' => 'btn btn-info'),
                'label' => Yii::t('user', 'Edit'),
                'url' => $this->editUrl,
                'imageUrl' => false,
                'options' => array(
                    'class' => 'btn btn-info',
                    'target' => '_blank',
                    'title' => Yii::t('user', 'Edit')
                ),
            );
        }
        if ($this->deleteUrl)
        {
            $this->buttons['delete'] = array(
                'icon' => 'icon-trash',
                'options' => array('class' => 'btn btn-danger'),
                'label' => Yii::t('user', 'Delete'),
                'imageUrl' => false,
                'options' => array(
                    'class' => 'btn btn-danger',
                    'title' => Yii::t('user', 'Delete')
                ),
            );
        }
        
        parent::init();
    }

    protected function renderButton($id, $button, $row, $data)
    {
        if (!$this->evaluateExpression($button['url'], array('data' => $data, 'row' => $row)))
            return TRUE;

        if (isset($button['visible']) && !$this->evaluateExpression($button['visible'], array('row' => $row, 'data' => $data)))
            return;
        $url = isset($button['url']) ? $this->evaluateExpression($button['url'], array('data' => $data, 'row' => $row)) : '#';
        $options = isset($button['options']) ? $button['options'] : array();

        //Add icon
        if (isset($button['icon']) && is_string($button['icon']))
            echo CHtml::link('<i class="' . $button["icon"] . '"></i>', $url, $options);
    }

}
